<?php

namespace addons\shopro\job;

use addons\shopro\service\order\OrderCreate;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\shopro\goods\SkuPrice;
use app\admin\model\shopro\lucky\Order;
use app\admin\model\shopro\order\OrderItem;
use app\admin\model\shopro\lucky\User;
use app\admin\model\shopro\user\Address;
use think\Db;
use think\exception\HttpResponseException;
use think\queue\Job;

class Luckey extends BaseJob
{

    public function luckey(Job $job, $data)
    {


        $server=$data['item'];
        $list=$data['list'];
        if(empty($list)){
            $job->delete();
            return false;
        }
        Db::startTrans();
        try{

            foreach ($list as $item){

                $item=Order::where(['id'=>$item['id'],'status'=>'0'])->find();
                if($item){
                    if($data['type']===1){
                        $item->status='1';
                        $score=$item['price']*$server['ok_multiplier'];
                        //WalletService::change($item['user_id'], 'score',$score,'lucky_income');
                        User::where(['user_id'=>$item['user_id'],'id'=>$item['lucky_user_id']])
                            ->inc('p_number',1)
                            ->inc('p_total_price',$item['price'])
                            ->inc('score',$score)->update();

                        $shop_lucky_num=Db::table('cj_shop_lucky_num')->where('user_id',$item['user_id'])->setInc('shop_num');
                        if(!$shop_lucky_num){
                            Db::table('cj_shop_lucky_num')->insert([
                                'user_id'=>$item['user_id'],
                                'shop_num'=>1,
                            ]);
                        }
                    }elseif($data['type']===2){
                        $item->status='2';
                        $commission=round($item['price']*$server['no_multiplier']*0.01,2);
                        //WalletService::change($item['user_id'], 'commission',$commission,'lucky_income');
                        User::where(['user_id'=>$item['user_id'],'id'=>$item['lucky_user_id']])
                            ->inc('gold_bean',$commission)->update();
                    }
                    $item->save();
                }

            }
            Db::commit();
            $job->delete();
        }catch (HttpResponseException $e) {
            Db::rollback();
            $data = $e->getResponse()->getData();

            $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
            format_log_error($e, 'luckey.HttpResponseException', $message);
        } catch (\Exception $e) {
            Db::rollback();

            format_log_error($e, 'Luckey');
        }


    }

    public function luckyShop(Job $job,$data)
    {
        try {

            $userLucky=User::where(['lucky_id'=>$data['lucky_id'],'stutas'=>'0'])->select();
            if($userLucky){
                foreach ($userLucky as $item){
                    \think\Queue::push('\addons\shopro\job\Commission@orderCommission', $item, 'shopro-high');
                }
            }
            $num=15;
            $goods_sku_price=SkuPrice::where(['goods_id'=>7])->find();
            Db::table('cj_shop_lucky_num')->chunk(20,function ($i) use ($num,$goods_sku_price){
                foreach ($i as $item){
                    if($item['shop_num']-$item['out_num']>=$num){
                        Db::startTrans();
                        try{
                            $address=Address::where(['user_id'=>$item['user_id'],'is_default'=>1])->find();
                            if($address){
                                $orderInfo=[
                                    'address_id'=>$address['id'],
                                    'goods_list'=>[
                                        ['goods_id'=>7,'goods_num'=>$num,'goods_sku_price_id'=>$goods_sku_price['id']]
                                    ],
                                    'order_type'=>'goods',
                                    'user_id'=>$item['user_id'],
                                ];

                                $orderCreate = new OrderCreate($orderInfo);

                                $result = $orderCreate->calc('create');

                                $oredr=$orderCreate->create($result);
                                $pay_order=\app\admin\model\shopro\order\Order::where(['id'=>$oredr['id']])->find();

                                $pay_order->pay_fee = 200*$num;

                                $pay_order->remain_pay_fee = 0;

                                $pay_order->paid_time = time();
                                $pay_order->status = 'paid';

                                $pay_order->save();

                                Db::table('cj_shop_lucky_num')->where('id',$item['id'])->setInc('out_num',$num);

                            }else{
                                print('地址不正确！');
                            }
                            Db::commit();
                        }catch (\Exception $e){
                            print($e->getMessage());
                            Db::rollback();
                        }
                    }
                }
            });
            $job->delete();
        }catch (HttpResponseException $e) {

            $data = $e->getResponse()->getData();
            $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
            format_log_error($e, 'luckey.HttpResponseException', $message);
        } catch (\Exception $e) {
            print($e->getMessage());
            format_log_error($e, 'Luckey');
        }


    }

}